// pages/video/video.js
import request from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navData: [],
    navId: '',
    videoList: [],
    vid: '',
    updateArr: [],
    isFresh: false
  },
  goSearch(){
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
  handleNav(e) {
    // console.log(e.currentTarget.id);
    // console.log(e.currentTarget.id*1);
    this.setData({
      navId: e.currentTarget.id,
      videoList: []
    })
    this.getVideoList(this.data.navId)
  },
  handlePlay(e) {
    let vid = e.currentTarget.id
    this.setData({
      vid
    })
    // this.vid != vid && this.context && this.context.stop()
    // this.vid = vid;
    this.context = wx.createVideoContext(vid)

    let {
      updateArr
    } = this.data
    let isExist = updateArr.find(item => item.vid == vid)
    if (isExist) {
      this.context.seek(isExist.currentTime)
    }
    this.context.play();
    // console.log(e);
  },
  timeupdate(event) {
    let vid = event.currentTarget.id
    let currentTime = event.detail.currentTime
    let {
      updateArr
    } = this.data
    let isExist = updateArr.find(item => item.vid == vid)
    if (isExist) {
      isExist.currentTime = currentTime
    } else {
      updateArr.push({
        vid,
        currentTime
      })
    }

    this.setData({
      updateArr
    })
  },
  ended(event) {
    let vid = event.currentTarget.id
    let {
      updateArr
    } = this.data
    let isExistIndex = updateArr.findIndex(item => item.vid == vid)
    updateArr.splice(isExistIndex, 1)
    this.setData({
      updateArr
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    let userInfo = wx.getStorageSync('userInfo')
    if (!userInfo) {
      // console.log(222);
      wx.redirectTo({
        url: '/pages/login/login'
      });
    }

    let res = await request('video/group/list')
    // console.log(res);
    this.setData({
      navData: res.data.slice(0, 20),
      navId: res.data[0].id
    })

    this.getVideoList(this.data.navId)
  },
  bindrefresherpulling() {
    this.getVideoList(this.data.navId)
  },
  bindscrolltolower() {
    console.log(222);
    let {
      videoList
    } = this.data;
    let newarr = [...videoList.slice(0, 8)];
    videoList.push(...newarr)
    this.setData({
      videoList
    })
  },
  async getVideoList(id) {
    wx.showLoading({
      title: '正在加載...'
    });
    let res = await request('video/group', {
      id
    })
    wx.hideLoading();
    this.setData({
      videoList: res.datas,
      isFresh: false
    })
    // console.log(res);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})